﻿#include "input.h"
#include "ui_input.h"

#include <QFile>

ModelSetting Input::FDsetting=ModelSetting("G:/APP1/OpenCV/SeetaFace/bin/model/fd_2_00.dat",ModelSetting::CPU, 0);
ModelSetting Input::FLMsetting=ModelSetting("G:/APP1/OpenCV/SeetaFace/bin/model/pd_2_00_pts5.dat",ModelSetting::CPU, 0);
ModelSetting Input::FDBsetting=ModelSetting("G:/APP1/OpenCV/SeetaFace/bin/model/fr_2_10.dat",ModelSetting::CPU, 0);

Input::Input(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Input) ,
    engine(FDsetting, FLMsetting, FDBsetting),
    faceTracker(FDsetting)
{
    ui->setupUi(this);
    setWindowTitle("信息录入系统");
    //打开摄像头
    capture.open(0);
    //开启定时器
    timerid = startTimer(100);
    this->FDPtr = &engine.FD;
    this->FLDPtr = &engine.PD;
    this->FDPtr->set(seeta::FaceDetector::PROPERTY_MIN_FACE_SIZE,80);
    //导出数据库
    engine.Load("F:/yueqian/sql/db/project/face.fdb");

    //初始化模型
    model = new QSqlTableModel();//默认数据库
    model->setTable("Personnel_information");//绑定数据库中对应表格

}

Input::~Input()
{
    delete ui;
}

//seeta来识别人脸
void Input::cheeck_Seeta(){
    capture.read(videoImage);
    if(videoImage.data == nullptr) return ;
    SeetaImageData image;  //<<===videoImage
    image.width = videoImage.cols;
    image.height = videoImage.rows;
    image.channels = videoImage.channels();
    image.data  =videoImage.data;
    //追踪
    SeetaTrackingFaceInfoArray  faceArray = faceTracker.track(image);
    for(int i=0; i<faceArray.size; i++)
    {
        SeetaTrackingFaceInfo faceInfo = faceArray.data[i];
        cv::Rect rect = {faceInfo.pos.x, faceInfo.pos.y, faceInfo.pos.width, faceInfo.pos.height};
        rectangle(videoImage,rect, Scalar(0,0,255));
    }
    //在Qt显示图片
    QImage imageq(videoImage.data, videoImage.cols, videoImage.rows, videoImage.step, QImage::Format_BGR888);
    QPixmap mmp = QPixmap::fromImage(imageq);
    ui->pix->setPixmap(mmp);
}
//定时器
void Input::timerEvent(QTimerEvent *event){
    cheeck_Seeta();
}
//保存
void Input::on_holdBt_clicked()
{
    //获取信息
    QString Number = ui->Number->text();
    QString Photo_name = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz");//当前时间
    QString Full_name = ui->Full_name->text();
    QString Gender = ui->Gender->text();
    QString Telephone = ui->Telephone->text();
    QString Birth = ui->Birth->text();
    QString Time = ui->Time->text();
    if (Number.isEmpty() || Photo_name.isEmpty() || Full_name.isEmpty() || Gender.isEmpty() ||
            Telephone.isEmpty() || Birth.isEmpty() || Time.isEmpty()){
        QMessageBox::information(this,"提示","请填写完所有信息!");
        return;
    }else{
        if (Gender != "男" && Gender != "女") {
            QMessageBox::information(this,"提示","输入正确的格式!");
            return;
        }
        else{
            //上传到服务器
            //保存到数据库
            //创建一条记录
            QSqlRecord record = model->record();
            //初始化
            record.setValue("Number",Number);
            record.setValue("Photo",Photo_name);
            record.setValue("Full_name",Full_name);
            record.setValue("Gender",Gender);
            record.setValue("Telephone",Telephone);
            record.setValue("Birth",Birth);
            record.setValue("Time",Time);
            if (!model->insertRecord(0,record))
            {
                QMessageBox::information(this,"失败","该人员编号已存在!");
            }
            else
            {
                QMessageBox::information(this,"成功","保存成功!");
            }
        }
    }
}

//查找
bool Input::select_user(QString number){
    model->setFilter(QObject::tr("Number='%1").arg(number));
    model->select();
    if (model->rowCount() == 0)
        return false;
    return true;
}

//开始
void Input::on_startBt_clicked()
{
    //开启定时器
    this->timerid = startTimer(100);
}
//停止
void Input::on_stopBt_clicked()
{
    killTimer(this->timerid);
}
//拍照
void Input::on_photoBt_clicked()
{
    killTimer(this->timerid);
    QString name = "./tup.bmp";
    imwrite(name.toUtf8().data(), videoImage);
    Mat rdata = imread(name.toUtf8().data());
    regData.width =  rdata.cols;
    regData.height = rdata.rows;
    regData.channels = rdata.channels();
    regData.data  = rdata.data;
    int id = engine.Register(regData);
    users[id] = ui->Number->text();
    //保存数据库
    engine.Save("F:/yueqian/sql/db/project/face.fdb");
    //开启定时器
    this->timerid = startTimer(100);
    QMessageBox::information(this,"提示","拍摄成功！");
}

//预览
void Input::on_lookBt_clicked()
{
    //打开图片
    QPixmap map("./tup.bmp");
    //创建空白对话框界面
    QDialog dialog(this);
    dialog.resize(map.size());
    QLabel label(&dialog);
    label.resize(dialog.size());
    label.setPixmap(map);
    dialog.exec();
}
//回退
void Input::on_pushButton_clicked()
{
    this->parentWidget()->show();
    delete this;
}
